package com.bocai.dahuatech.ui.activity;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;

import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.event.MainEvent;
import com.bocai.dahuatech.ui.frag.FragmentMyOrder;
import com.bocai.dahuatech.utils.SharePrefencesUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我的订单列表
 */
public class MyOrderListActivity extends BaseActivity
{
    @Bind(R.id.order_tab)
    TabLayout mTabLayout;
    @Bind(R.id.order_vp)
    ViewPager viewPager;
    @Bind(R.id.ll_myorder_nocontent)
    LinearLayout llNoCotent;
    String ordertypeall,ordertypeready,ordertypeno,ordertypealready,ordertypecomplete;
    @Override
    protected int contentView()
    {
        return R.layout.activity_my_order_list;
    }

    @Override
    protected void initView()
    {
        ButterKnife.bind(this);
        initToolbar(R.string.meorder);
        if(SharePrefencesUtil.getFAccessFlag(this)==2)
        {
            llNoCotent.setVisibility(View.GONE);
            ordertypeall=getString(R.string.ordertypeall);
            ordertypeready=getString(R.string.ordertypeready);
            ordertypeno=getString(R.string.ordertypeno);
            ordertypealready=getString(R.string.ordertypealready);
            ordertypecomplete=getString(R.string.ordertypecomplete);
            viewPager.setAdapter(new Adapter(getSupportFragmentManager()));
            mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
            viewPager.setOffscreenPageLimit(4);
            mTabLayout.setupWithViewPager(viewPager);
            initEvent();

        }else
        {
            llNoCotent.setVisibility(View.VISIBLE);
        }

    }


    @Override
    public void onEventMainThread(MainEvent event)
    {
        super.onEventMainThread(event);
        if(event.msg.equals("goshopcart"))
        {
           finish();
        }
    }

    @Override
    protected void initEvent()
    {
        super.initEvent();
            mToolbar.inflateMenu(R.menu.menu_myorder);
            mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener()
            {
                @Override
                public boolean onMenuItemClick(MenuItem item)
                {
                    startActivity(new Intent(MyOrderListActivity.this, SearchMyOrderActivity.class));
                    return false;
                }
            });
    }
     class Adapter extends FragmentPagerAdapter
    {
        private final Fragment[] mFragments = new Fragment[5];
        private final String [] mFragmentTitles = {ordertypeall,ordertypeready,ordertypeno,ordertypealready,ordertypecomplete};

        public Adapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position)
        {
            Fragment fragment = mFragments[position];
            if (fragment == null)
            {
                fragment = new FragmentMyOrder(mFragmentTitles[position]);
                mFragments[position] = fragment;
            }
            return fragment;
        }

        @Override
        public int getCount()
        {
            return 5;
        }

        @Override
        public CharSequence getPageTitle(int position)
        {

            return mFragmentTitles[position];
        }
    }
}
